<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户性别</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sex" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户城市</label>
                            <div class="layui-input-inline">
                                <input type="text" name="city" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户职业</label>
                            <div class="layui-input-inline">
                                <input type="text" name="classify" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script type="text/html" id="switchTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="sex" value="{{d.id}}" class="" lay-skin="switch" lay-text="正常|关闭" lay-filter="sexDemo"
           {{ d.status== {$result.sql_success} ? 'checked' : '' }}>
</script>
<script type="text/html" id="iconTpl">
    <img src="/{{d.icon}}" onerror="javascript:this.src='';">
</script>
<script>
    layui.config({
        base: '/static/plugs/lay_ext/tableTree/'
    }).extend({
        tableTree: 'tableTree'
    })
    layui.use(['form', 'table', 'tableTree'], function () {
        const $ = layui.jquery,
            form = layui.form,
            table = layui.table
            , tableTree = layui.tableTree;
        let treeTable = tableTree.render({
            elem: '#currentTableId'
            , id: 'tableTree'
            , url: "{:url('index')}"
            , height: 'full-90'
            , size: 'sm'
            , limits: [10, 20]
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_SEARCH',
                icon: 'layui-icon-search'
            }]
            , page: true
            , treeConfig: { //表格树所需配置
                showField: 'title' //表格树显示的字段
                , treeid: 'id' //treeid所对应字段的值在表格数据中必须是唯一的，且不能为空。
                , treepid: 'pid'//父级id字段名称
                , iconClass: 'layui-icon-right' //小图标class样式 窗口图标 layui-icon-layer
                , showToolbar: true //展示工具栏 false不展示 true展示
            }
            , cols: [[
                {type: 'checkbox'}
                , {field: 'title', title: '名称', width: 300}
                , {field: 'sort', title: '排序', event: 'sort', width: 70, config: {type: 'input'}}
                , {field: 'icon', title: '图标', templet: '#iconTpl',unresize: true, width: 120,event:'icon'}
                , {
                    field: 'create_time',
                    title: '创建时间',
                    event: 'date',
                    width: 200,
                }, {
                    field: 'status',
                    title: '状态',
                    event: 'status',
                    templet: '#switchTpl',
                    unresize: true
                }
            ]], done: function () {
                treeTable.closeAllTreeNodes();
                treeTable.openTreeNode(1)
            }
            , response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: "{$result.success}" //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
        });
        tableTree.on('tool(currentTableFilter)', function (obj) {

            const field = obj.field; //单元格字段
            const value = obj.value; //修改后的值
            const data = obj.data; //当前行数据
            const event = obj.event; //当前单元格事件
            //console.log(obj)
            if (event === 'edit') {
                updateEditValue(data.id, value, field)
            }
            if (event === 'sort') {
                updateEditValue(data.id, value, field)
            }
            if(event === 'icon'){
                //tao.open();
            }
            if (event === 'status') {
                //console.log(data.status)
                let value = "";
                if (data.status == "{$result.sql_success}") {
                    value = "{$result.sql_error}";
                } else {
                    value = "{$result.sql_success}"
                }
                updateEditValue(data.id, value, field)
            }
            //event为del为删除 add则新增 async则异步请求数据
            if (event === 'del') {
                updateEditValue(data.id,"{$result.sql_delete}",'status')
            }
            if (event === 'add') { //点击操作栏加号图标时触发
                //异步、同步都可以使用
                //obj.add(arr)生成表格树,arr参数为数组，数组中元素的treeid字段值重复则被过滤掉
                // obj.add([]) //参数不传或为空数组时 => 新增空行
                tao.open("新增", "{:url('add')}?pid=" + data.id, "600", "400")
            }
            if (event === 'async') { //点击方向箭头小图标时触发
                //可ajax异步请求后台数据,回调obj.async(arr)生成表格树,arr参数为数组
                //数组中元素的treeid字段值重复则被过滤掉
                obj.async([{
                    "id": 'abc',
                    "treeName": 'abc',
                    "permissionId ": 'abc',
                    "sort": '3333',
                    createDate: '2020-02-02',
                    type: '1'
                }]);
            }
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            const result = data.field;

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            //console.log(obj)
            if (obj.event === 'add') {  // 监听添加操作
                const index = layer.open({
                    title: '新增分类',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['600px', '400px'],
                    content: "{:url('add')}",
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作

                const checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            } else if (obj.event === 'LAYTABLE_SEARCH') {
                $(".table-search-fieldset").slideToggle();
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        

    });

</script>